package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IWgtsHomePageFragmentModel;
import com.wgts.wgts.mvp.presenter.IWgtsHomePageFragmentPresenter;
import com.wgts.wgts.mvp.view.IWgtsHomePageFragmentView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.AppModleRequest;
import com.wgts.wgts.netmodle.resp.AppModleEntity;
import com.wgts.wgts.netmodle.resp.ArticleEntity;

import java.util.List;

public class WgtsHomePageFragmentPresenter implements IWgtsHomePageFragmentPresenter {

    private IWgtsHomePageFragmentModel model;

    private IWgtsHomePageFragmentView view;

    private int pageIndex = 1;

    private int pageSize = 20;

    public WgtsHomePageFragmentPresenter(IWgtsHomePageFragmentModel model, IWgtsHomePageFragmentView view) {
        this.model = model;
        this.view = view;
    }

    @Override
    public void initArtrict() {
        pageIndex = 1;
        model.wgtsArticle(String.valueOf(pageIndex), String.valueOf(pageSize), new HttpResultSubscriber<List<ArticleEntity>>() {
            @Override
            public void onSuccess(List<ArticleEntity> articleEntities) {
                if(articleEntities.size() < pageSize){
                    view.initArtric(articleEntities, true);
                }else{
                    view.initArtric(articleEntities, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void moreMoreArtric() {
        model.wgtsArticle(String.valueOf(pageIndex), String.valueOf(pageSize), new HttpResultSubscriber<List<ArticleEntity>>() {
            @Override
            public void onSuccess(List<ArticleEntity> articleEntities) {
                if(articleEntities.size() < pageSize){
                    view.moreArtric(articleEntities, true);
                }else{
                    view.moreArtric(articleEntities, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }


    @Override
    public void initDataView() {
        AppModleRequest appModleRequest = new AppModleRequest();
        appModleRequest.scene = "0";
        appModleRequest.parentTypeId = -1;
        model.initDataView(appModleRequest, new HttpResultSubscriber<List<AppModleEntity>>() {
            @Override
            public void onSuccess(List<AppModleEntity> appModleEntities) {
                view.initResult(appModleEntities);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }
}
